package top.superflow.utils

import java.lang.reflect.Method

import top.superflow.core.utils.PojoUtils
import top.superflow.exception.{Exceptable, RequestUnsupportedException}
import top.superflow.lang.{CollectionUtils, StringUtils}
import top.superflow.log.Loggable

object MetadataUtils extends Exceptable with Loggable{

  final val META_DATA_NAME_CLASS = "class"

  final val META_DATA_NAME_METHOD = "method"

  final val META_DATA_NAME_CONDITION = "condition"

  final val META_DATA_NAME_PARAM_TYPES = "paramTypes"

  def getParameters(metadata: Map[String, Any]): Array[String] = {

    val parameters = get(metadata, META_DATA_NAME_PARAM_TYPES)

    if (parameters == null) {
      return null
    }

    if (parameters.isInstanceOf[Array[String]]) {
      return parameters.asInstanceOf[Array[String]]
    }

    if (parameters.isInstanceOf[List[String]]) {
      return parameters.asInstanceOf[List[String]].toArray
    }

    return null
  }

  def getClass(metadata: Map[String, Any]): Class[_] = {

    val clz = get(metadata, META_DATA_NAME_CLASS)

    if (clz == null) {
      logger.warn("There is no corresponding item for {} in the meta data", META_DATA_NAME_CLASS)
      return null
    }

    if (clz.isInstanceOf[String] && ClassUtils.isClass(clz.toString)) {
      return ClassUtils.getClass(clz.toString)
    }

    return null
  }


  def get(metadata: Map[String, Any], metadataName: String): Any = {
    if (CollectionUtils.isEmpty(metadata) || StringUtils.isBlank(metadataName)) {
      return null
    }

    if (!metadata.contains(metadataName)) {
      return null
    }

    return metadata.get(metadataName).get
  }
}
